//
//  YPAtuoDetailViewController.m
//  ChengHuiTong
//
//  Created by ypwl on 16/8/12.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "YPAtuoDetailViewController.h"
#import "YPAtuoDetailTableViewCell.h"
#import "YPATSelectViewController.h"
@interface YPAtuoDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation YPAtuoDetailViewController{
    
    YPautoEditBin*_model;
    
    UITableView*_autoDetail;
    
    NSArray*_titleArr;
    
    NSArray*_contentArr;
    
    NSDictionary*_modelDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=UIColorFromString(@"#F6F6F6");
    self.title=@"自动投标";
    _titleArr=@[@"投标金额:",@"还款方式:",@"借款期限:",@"标种类型:",@"有效时间:",@"是否有效:",@"当前排名:"];
    
    
    [self creatTableView];
    
    [self getData];
    
    // Do any additional setup after loading the view.
}

-(void)getData{
    
    YPautoDetailBin*bin=[[YPautoDetailBin alloc] init];
    
    bin.token=[YPShareAccountInfo shareAccountInfo].token;
    
    bin.autoid=self.Bid;
    
    [YPHttpRequestMethod sendAsyRequestURL:autoDetail withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        
      
        NSDictionary*dic2=@{
                            @"1":@"全部",
                            @"3":@"先息后本",
                            @"0":@"等额本息",
                            @"4":@"等额本金"
                            };
        NSDictionary*dic3=@{
                            @"0":@"全部",
                            @"1":@"诚信贷",
                            @"2":@"诚车贷",
                            @"5":@"加信贷",
                            };
        NSDictionary*dic4=@{
                            @"1":@"是",
                            @"0":@"否",
                            };

        
        if ([responseDictionary[@"status"] intValue]==0) {
            
            _model=[[YPautoEditBin alloc] initWithDic:responseDictionary];
            
            _modelDic=responseDictionary;
           
            _contentArr=@[[self getMoneyWithModel:_model],dic2[_model.borrow_style],[NSString stringWithFormat:@"%@%@",_model.timelimit,@"个月"],dic3[_model.borrow_type],[NSString stringWithFormat:@"%@——%@",_model.validity_start,_model.validity_end], dic4[_model.enabled],_model.sort];
            
            [_autoDetail reloadData];
        }
        
    } withFailedBlock:^(NSError *error) {
        
    } withIndicator:NO atView:self.view];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _titleArr.count;
}
-(void)creatTableView{
    
    _autoDetail = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    
    _autoDetail.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _autoDetail.backgroundColor = UIColorFromString(@"#F6F6F6");
    
    _autoDetail.showsVerticalScrollIndicator = NO;
    
    _autoDetail.rowHeight=30;
    
    _autoDetail.delegate = self;
    
    _autoDetail.dataSource = self;
    
    _autoDetail.tableFooterView=[self footView];
    
    [self.view addSubview:_autoDetail];

}

-(UIView*)footView{
    
    UIView*footView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 65)];
    
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    
    leftButton.frame=CGRectMake(20, 15, (SCREEN_WIDTH-40-15)/2.0, 35);
    
    [leftButton setTitle:@"编辑" forState:0];
    
    [leftButton setTitleColor:[UIColor whiteColor] forState:0];
    
    [leftButton setBackgroundColor:kColor_AppNavColor];
    
    [leftButton addTarget:self action:@selector(editBid) forControlEvents:UIControlEventTouchUpInside];
    
    [footView addSubview:leftButton];
    
    
    UIButton*rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
    
    rightButton.frame=CGRectMake(leftButton.right+15, 15, leftButton.width, 35);
    
    [rightButton setTitle:@"删除" forState:0];
    
    [rightButton setTitleColor:UIColorFromString(@"#AEAEAE") forState:0];
    
    [rightButton setBackgroundColor:[UIColor whiteColor]];
    
    rightButton.layer.borderColor=[UIColor grayColor].CGColor;
    
    rightButton.layer.borderWidth=0.5;
    
     [rightButton addTarget:self action:@selector(deleteBid) forControlEvents:UIControlEventTouchUpInside];
    
    [footView addSubview:rightButton];
    
    
    
    return footView;
    
    
}

-(void)editBid{
    
    YPATSelectViewController*editVC=[[YPATSelectViewController alloc] init];
    
    editVC.model=[[YPautoEditBin alloc] initWithDic:_modelDic];
    
  
    
    editVC.getNewData=^{
        
      
        [self getData];
        
    };
    
    [self.navigationController pushViewController:editVC animated:YES];
}

-(void)deleteBid{
    
    YPautoDelBin*bin=[[YPautoDelBin alloc] init];
    
    bin.token=[YPShareAccountInfo shareAccountInfo].token;
    
    bin.autoid=_model.autoid;
    
    [YPHttpRequestMethod updateRequestURL:autoDel withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        
        if ([responseDictionary[@"status"] intValue]==0) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
    } withFailedBlock:^(NSError *error) {
        
    } withIndicator:YES atView:self.view];
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YPAtuoDetailTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        
        cell=[[YPAtuoDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        cell.selectionStyle=0;
        
    }
    
    
    cell.titleLabel.text=_titleArr[indexPath.row];
    
    if (_contentArr) {
        
        cell.contentLabel.text=_contentArr[indexPath.row];
    }
    
    
    
    return cell;
    
}


-(NSString*)getMoneyWithModel:(YPautoEditBin*)model{
    
    
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
    formatter.numberStyle = NSNumberFormatterRoundFloor;
    
    
   
    
   
    
   
   
    
    
    return  [NSString stringWithFormat:@"￥%@~￥%@",[formatter stringFromNumber:[NSNumber numberWithFloat:[model.min_account floatValue]]],[formatter stringFromNumber:[NSNumber numberWithFloat:[model.tender_account floatValue]]]];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
